/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: Music.java 564 2010-07-25 20:54:57Z Harry $
 *   $LastChangedDate: 2010-07-25 20:54:57 +0000 (Sun, 25 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.resource;

import java.io.IOException;
import java.net.URL;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

/**
 *
 * @author Harry Nguyen
 */
public enum Music {

    GAME_MENU("/battlefield/sounds/start.mid"),
    LOADING("/battlefield/sounds/load.mid"),
    TACTICAL_PHASE("/battlefield/sounds/tactical.mid"),
    WAR_PHASE("/battlefield/sounds/bat.mid");
    private URL musicURL;
    private Sequencer sequencer;

    Music(String musicFileName) {
        try {
            musicURL = this.getClass().getResource(musicFileName);
            Sequence sequence = MidiSystem.getSequence(musicURL);
            sequencer = MidiSystem.getSequencer();
            sequencer.setSequence(sequence);
            sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
            sequencer.open();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InvalidMidiDataException ex) {
            System.out.println(ex.toString());
        } catch (MidiUnavailableException ex) {
            System.out.println(ex.toString());
        }
    }

    public void start() {
        try {
            sequencer.setTickPosition(sequencer.getLoopStartPoint());
            sequencer.start();
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public void stop() {
        try {
            sequencer.stop();
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public boolean isRunning() {
        return sequencer.isRunning();
    }
}
